<template>
  <div class="graph" id="pie_graph"></div>
</template>

<script>
import * as echarts from "echarts/core";
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent,
} from "echarts/components";
import { PieChart } from "echarts/charts";
import { LabelLayout } from "echarts/features";
import { CanvasRenderer } from "echarts/renderers";

echarts.use([
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  PieChart,
  CanvasRenderer,
  LabelLayout,
]);
export default {
  name: "Pie",
  props: ["pieData"],
  mounted() {
    const chartDom = document.getElementById("pie_graph");
    var myChart = echarts.init(chartDom);
    var option;

    option = {
      title: {
        text: "test",
        subtext: "Fake Data",
        left: "center",
      },
      tooltip: {
        trigger: "item",
      },
      legend: {
        orient: "vertical",
        left: "left",
      },
      series: [
        {
          name: "Access From",
          type: "pie",
          radius: "50%",
          data: [
            { value: 1048, name: "Search Engine" },
            { value: 735, name: "Direct" },
            { value: 580, name: "Email" },
            { value: 484, name: "Union Ads" },
            { value: 300, name: "Video Ads" },
          ],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: "rgba(0, 0, 0, 0.5)",
            },
          },
        },
      ],
    };
    if (this.pieData) {
      const { title, data } = this.pieData;
      option.title.text = title;
      option.series[0].data = data;
    }
    option && myChart.setOption(option);
  },
};
</script>

<style lang='less' scoped>
#pie_graph {
  width: 100%;
  height: 100%;
}
</style>